'use strict';

module.exports = app => {
  const { INTEGER, STRING, TEXT } = app.Sequelize;

  const OutfitItem = app.model.define('outfit_item', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    outfit_id: {
      type: INTEGER,
      allowNull: false,
      comment: '搭配ID',
    },
    clothing_id: {
      type: INTEGER,
      allowNull: false,
      comment: '衣服ID',
    },
    position: {
      type: STRING(20),
      allowNull: true,
      comment: '在搭配中的位置(top, bottom, outer, shoes, accessories)',
    },
    layer_order: {
      type: INTEGER,
      defaultValue: 1,
      comment: '层次顺序',
    },
    is_main: {
      type: app.Sequelize.BOOLEAN,
      defaultValue: false,
      comment: '是否为主要单品',
    },
    notes: {
      type: TEXT,
      allowNull: true,
      comment: '备注',
    },
    // 时间戳字段由Sequelize自动管理
  }, {
    tableName: 'outfit_items',
    paranoid: false,  // 禁用软删除
    comment: '搭配项目关联表',
    indexes: [
      {
        fields: ['outfit_id'],
      },
      {
        fields: ['clothing_id'],
      },
      {
        unique: true,
        fields: ['outfit_id', 'clothing_id'],
      },
    ],
  });

  OutfitItem.associate = function() {
    // 搭配项目属于搭配
    app.model.OutfitItem.belongsTo(app.model.Outfit, {
      foreignKey: 'outfit_id',
      as: 'outfit',
    });

    // 搭配项目属于衣服
    app.model.OutfitItem.belongsTo(app.model.Clothing, {
      foreignKey: 'clothing_id',
      as: 'clothing',
    });
  };

  return OutfitItem;
};
